package turtle.system.invest;

import java.io.InputStream;
import java.util.HashMap;
import java.util.NoSuchElementException;

import org.osite.util.ResourceLoader;

import turtle.system.invest.rule.InvestHistory;

public class Environment implements ResourceLoader {

    public static final String WTX0 = "wtx0";
    public static final String WTX1 = "wtx1";

    private int timestamp;

    private final HashMap<String, Object> map = new HashMap<String, Object>();
	private InvestHistory investHistory = new InvestHistory();

    private ResourceLoader resources;
    
    @SuppressWarnings("unchecked")
	public <T> T find(String name) {
    	return (T) map.get(name);
    }

	public <T> T get(String name) {
        @SuppressWarnings("unchecked")
        T result = (T) map.get(name);
        if (result == null) {
            throw new NoSuchElementException(name);
        }
        return result;
	}

    public <T> T get(String name, T defaultValue) {
        @SuppressWarnings("unchecked")
        T result = (T) map.get(name);
        if (result == null) return defaultValue;
        return result;
    }

	@SuppressWarnings("unchecked")
    public <T> T set(String name, T object) {
	    return (T) map.put(name, object);
	}

	public int getTimestamp() {
		return timestamp;
	}
	
	public InvestHistory resetInvestHistory() {
		investHistory = new InvestHistory();
		return investHistory;
	}
	
	public InvestHistory getInvestHistory() {
		return investHistory;
	}

    @Override
    public InputStream getResource(String path) {
        return resources == null ? null : resources.getResource(path);
    }

    @Override
    public String[] listResources(String path) {
        return resources == null
                ? new String[0] : resources.listResources(path);
    }

    public void setTimestamp(int timestamp) {
        this.timestamp = timestamp;
    }

    public void setResourceLoader(ResourceLoader loader) {
        this.resources = loader;
    }

}
